package com.zycfc.zsf.boot.exception;

import com.zycfc.zsf.boot.util.text.*;
import org.slf4j.*;

public abstract class BaseException extends RuntimeException
{
    private static final Logger log;
    private static final long serialVersionUID = 6626868446456726497L;
    private static final String PARAM_PRE = "{";
    private static final String PARAM_SUF = "}";
    private final IExceptionCode code;
    private final String codeMsg;
    
    public BaseException(final IExceptionCode code) {
        super((String)code.getValue() + "##@##" + code.getMessage());
        this.code = code;
        this.codeMsg = code.getMessage();
    }
    
    public BaseException(final IExceptionCode code, final String... params) {
        super((String)code.getValue() + "##@##" + code.getMessage());
        final String message = assembleRetMsg(code, params);
        this.code = code;
        this.codeMsg = message;
    }
    
    public BaseException(final IExceptionCode code, final String message) {
        super((String)code.getValue() + "##@##" + code.getMessage());
        this.code = code;
        this.codeMsg = message;
    }
    
    public BaseException(final IExceptionCode code, final Throwable throwable) {
        super((String)code.getValue() + "##@##" + code.getMessage(), throwable);
        this.code = code;
        if (null == throwable && null != code) {
            this.codeMsg = code.getMessage();
        }
        else if (null != throwable) {
            this.codeMsg = throwable.getMessage();
        }
        else {
            this.codeMsg = null;
        }
    }
    
    public IExceptionCode getCode() {
        return this.code;
    }
    
    @Override
    public String getMessage() {
        return (String)this.code.getValue() + "##@##" + this.code.getMessage();
    }
    
    public String getCodeMsg() {
        return this.codeMsg;
    }
    
    private String assembleMsg(final IExceptionCode code, final String[] paramArr) {
        String msg = code.getMessage();
        if (StringUtils.isEmpty(msg)) {
            return msg;
        }
        if (!msg.contains("{") || !msg.contains("}")) {
            return msg;
        }
        final String[] msgArr = msg.split("}");
        int fromIndex = 0;
        Integer paramIndex = null;
        Integer paramKey = null;
        String paramStr = null;
        final int maxIndex = paramArr.length - 1;
        for (int paramCount = msg.endsWith("}") ? msgArr.length : (msgArr.length - 1), i = 0; i < paramCount; ++i) {
            paramIndex = msg.indexOf("{", fromIndex) + 1;
            paramKey = Integer.parseInt(msg.substring(paramIndex, paramIndex + 1));
            if (paramKey > maxIndex) {
                BaseException.log.error("\u66ff\u6362\u54cd\u5e94\u63cf\u8ff0\u6570\u7ec4\u4e0b\u6807\u8d8a\u754c\uff0c\u66ff\u6362\u7d22\u5f15:{}\uff0c\u6570\u7ec4\u6700\u5927\u7d22\u5f15:{}\uff0c\u54cd\u5e94\u63cf\u8ff0:{}", new Object[] { paramKey, maxIndex, msg });
                return msg;
            }
            paramStr = "{" + paramKey + "}";
            if (msg.contains(paramStr)) {
                msg = msg.replace(paramStr, paramArr[paramKey]);
            }
            fromIndex = paramIndex + 2;
        }
        return msg;
    }
    
    private static String assembleRetMsg(final IExceptionCode code, final String... params) {
        String msg = code.getMessage();
        if (msg == null) {
            return msg;
        }
        if (!msg.contains("{") || !msg.contains("}")) {
            return msg;
        }
        final String[] msgArr = msg.split("}");
        int fromIndex = 0;
        Integer paramIndex = null;
        final int maxIndex = params.length - 1;
        for (int paramCount = msg.endsWith("}") ? msgArr.length : (msgArr.length - 1), i = 0; i < paramCount; ++i) {
            paramIndex = msg.indexOf("{", fromIndex);
            if (i > maxIndex) {
                BaseException.log.error("\u66ff\u6362\u54cd\u5e94\u63cf\u8ff0\u6570\u7ec4\u4e0b\u6807\u8d8a\u754c\uff0c\u66ff\u6362\u7d22\u5f15:{}\uff0c\u5165\u53c2\u6570\u7ec4\u6700\u5927\u7d22\u5f15:{}\uff0c\u54cd\u5e94\u63cf\u8ff0:{}", new Object[] { i, maxIndex, msg });
                return msg;
            }
            if (msg.length() - paramIndex > 2) {
                msg = msg.substring(0, paramIndex) + params[i] + msg.substring(paramIndex + 2);
            }
            else {
                msg = msg.substring(0, paramIndex) + params[i];
            }
            fromIndex = paramIndex + params[i].length() - 1;
        }
        return msg;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)BaseException.class);
    }
}
